<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <%@include file="../../../includes/common.jsp" %>
    <title>上传附件</title>
</head>
<body>
<div>
    <form id="filegetup" method="post" enctype="multipart/form-data" class="form-horizontal">
        <fieldset>
            <legend></legend>
            <div class="control-group">
                <label class="control-label" for="courseName">课件名称</label>

                <div class="controls">
                    <input type="text" name="courseName" id="courseName" value="${course.courseName}"/>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="courseType">课件类型</label>

                <div class="controls">
                    <input type="text" id="courseType" readonly value="${course.showCourseType}"
                           onclick="showCandidate();"/>
                    &nbsp;<a id="menuBtn" onclick="showCandidate(); return false;"><spring:message code="select"/></a>
                    <input type="hidden" id="typeId" name="totalNum" value="${course.courseType}">
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="fileImg">上传图像</label>

                <div class="controls">
                    <input type="file" name="fileImg" id="fileImg" value=""/>
                    <input type="hidden" name="Img" id="Img" value="${course.photoUrl}"/>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label"></label>

                <div class="controls">
                    <input type="button" id="button" onclick="fileUpAttachment()" class="btn btn-primary" value="确定"/>
                    <input type="button" onclick="quit()" class="btn btn-primary"
                           value="取消"/>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<div id="typeContent" class="divauto" style=" display:none; position: absolute;">
    <ul id="menuTree" class="ztree"></ul>
</div>
</body>
</html>
<script>
    var rules = {
        objInfo: {
            courseName: {
                checkEmpty: ["courseName", "课件名称"]
            },
            courseType: {
                checkEmpty: ["courseType", "课件类型"]
            }
        }
    };
    function fileUpAttachment() {
        if (!JC.validate(rules)) return;
        var url = document.getElementById("fileImg").value;
        if(url!=""){
            $("#Img").attr("value", "");
        }
        $("#filegetup").attr("action", basePath + "course/editCourseInfo?id=" +${course.id}).submit();
    }

    function quit() {
        parent.$("#dataEdit").dialog('close');
    }

    var setting = {
        view: {
            expandSpeed: "fast"
        },
        check: {
            enable: true,
            chkStyle: "radio",
            chkboxType: { "Y": "ps", "N": "ps" },
            radioType: "all"
        },
        data: {
            simpleData: {
                enable: true
            },
            key: {
                checked: "isChecked",
                name: "name"
            },
            keep: {
                parent: true
            }
        },
        callback: {
            beforeClick: beforeClick,
            onCheck: onCheckGroupCandidate
        }
    };

    var zNodes = [
        {id: "-1", name: "类型"},
        <c:forEach var="MenuPerm1" items="${typeList}">
        {id: "${MenuPerm1.typeName}", pId: "-1", name: "${MenuPerm1.typeName}"},
        </c:forEach>
        <c:forEach var="MenuPerm2" items="${typeValuesList}">
        {id: "${MenuPerm2.id}", pId: "${MenuPerm2.typeName}", name: "${MenuPerm2.typeValue}"},
        </c:forEach>
    ];

    function beforeClick(treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("menuTree");
        zTree.checkNode(treeNode, !treeNode.checked, null, true);
        return false;
    }

    function onCheckGroupCandidate(e, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("menuTree"),
                nodes = zTree.getCheckedNodes(true),
                type = "",
                typeId = "";
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].pId != -1) {
                type += nodes[i].name;
                typeId = nodes[i].id;
            }
            else {
                $.messager.alert("提示", "请选择叶子节点！", "", function () {
                });
            }
        }
        $("#courseType").attr("value", type);
        $("#typeId").attr("value", typeId);
    }

    function showCandidate() {
        var obj = $("#courseType");
        var objOffset = obj.offset();
        var width = obj.width() + 10;
        $("#typeContent").css({width: width + 'px', left: objOffset.left + "px", top: objOffset.top + obj.outerHeight() + "px"}).slideDown("fast");
        $("body").unbind("mousedown").bind("mousedown", onBodyDown);
    }

    function hideMenu() {
        $("#typeContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }

    function onBodyDown(event) {
        if (!(event.target.id == "menuBtn" || event.target.id == "courseType" || event.target.id == "typeContent" || $(event.target).parents("#typeContent").length > 0)) {
            hideMenu();
        }
    }

    $(document).ready(function () {
        $.fn.zTree.init($("#menuTree"), setting, zNodes);
        var treeObj = $.fn.zTree.getZTreeObj("menuTree");
    });

</script>